package com.ktjiaoyu.controller;

import com.ktjiaoyu.entity.Category;
import com.ktjiaoyu.entity.Entry;
import com.ktjiaoyu.service.CategoryService;
import com.ktjiaoyu.service.EntryService;
import com.ktjiaoyu.utils.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 黄军艳
 * @version 1.0
 * @date 2024/12/27  10:34
 */
@Controller
@RequestMapping("/main/edoc")
public class EntryController extends BaseController {
    private static final Logger LOGGER =
            LoggerFactory.getLogger(SysUserController.class);
    @Autowired
    private EntryService entryService;
    @Autowired
    private CategoryService categoryService;

    @RequestMapping("/page")
    public String page(Model model, Entry condition,
                       @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
                       @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize){
        Page<Entry> page=null;
        try {
            page=entryService.findPage(condition, pageNo, pageSize);
            List<Category> categories=categoryService.findAll();
            model.addAttribute("pageInfo", page);
            model.addAttribute("cateforyList",categories);
            model.addAttribute("condition",condition);
        }catch (Exception e) {
            LOGGER.error("EntryController 中的 page 方法执行发生错误。 ", e);
            throw new RuntimeException(e);
        }
        return "edoc/page";
    }

    @GetMapping("/save")
    public String tosave(Model model){
        List<Category> categories=null;
        try {
            categories=categoryService.findAll();
            model.addAttribute("categories",categories);
        }catch (Exception e) {
            LOGGER.error("EntryController 中的 tosave 方法执行发生错误。 ", e);
            throw new RuntimeException(e);
        }
        return "edoc/save";
    }

    @PostMapping("/save")
    public String dosave(Entry entry){
        try {
            boolean ok=false;
            if (entry.getId()==null){
                ok=entryService.insert(entry);
            }else {
                ok=entryService.update(entry);
            }
            if (ok){
                return redirect("/main/edoc/page");
            }
        }catch (Exception e) {
            LOGGER.error("EntryController 中的 dosave 方法执行发生错误。 ", e);
            throw new RuntimeException(e);
        }
        return "edoc/save";
    }

    @GetMapping("/edit")
    public String edit(Model model,@RequestParam("id") Long id){
        try {
            Entry entry=entryService.selectById(id);
            List<Category> categories=categoryService.findAll();
            model.addAttribute("entry",entry);
            model.addAttribute("categories",categories);
        }catch (Exception e) {
            LOGGER.error("EntryController 中的 edit 方法执行发生错误。 ", e);
            throw new RuntimeException(e);
        }
        return "edoc/save";
    }

    @GetMapping("/view/{id}")
    public String view(@PathVariable("id") Long id,Model model){
        try {
            Entry entry=entryService.selectById(id);
            model.addAttribute("entry",entry);
        }catch (Exception e) {
            LOGGER.error("EntryController 中的 view 方法执行发生错误。 ", e);
            throw new RuntimeException(e);
        }
        return "edoc/view";
    }

    @GetMapping("/titleExist")
    @ResponseBody
    public Object titleExist(@RequestParam("title") String title){
        Map<String,Object> map=new HashMap<>();
        try {
            Entry entry=entryService.selectByTitle(title);
            if (entry!=null){
                map.put("exist",1);
            }else {
                map.put("exist",0);
            }
        }catch (Exception e) {
            LOGGER.error("EntryController 中的 titleExist 方法执行发生错误。 ", e);
            throw new RuntimeException(e);
        }
        return map;
    }

    @DeleteMapping("/del/{id}")
    @ResponseBody
    public Object delete(@PathVariable("id") Long id){
        Map<String,Object> map=new HashMap<>();
        boolean ok=false;
        try {
            ok=entryService.delete(id);
            if (ok){
                map.put("code",200);
            }else {
                map.put("code",500);
            }
        }catch (Exception e) {
            LOGGER.error("EntryController 中的 titleExist 方法执行发生错误。 ", e);
            map.put("code",500);
            map.put("msg","删除失败");
        }
        return map;
    }
}
